package Dao;

import bean.customer;

public class UserDao extends BaseDao{
    /*
    * 根据用户名查询用户信息
    * return null 说明没有这个用户,反之亦然
    * */
    public customer queryUserByUsername(String username){
        String sql = "select pid,name,phonenumber,password from customer where name = ?";
        try {
            return queryForOne(customer.class,sql,username);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public customer queryUserByUsernameAndPassword(String phonenumber,String password){
        String sql = "select pid,name,phonenumber,password from customer where phonenumber = ? and password = ?";
        try {
            return queryForOne(customer.class,sql,phonenumber,password);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public String queryNameByPhoneNumberAndPassword(String phonenumber,String password) throws Exception {
        String sql = "select name  from customer where phonenumber = ? and password = ?";
        return (String) queryForSingleValue(sql,phonenumber,password);

    }

    public Integer queryIdByPhoneNumberAndPassword(String phonenumber,String password) throws Exception {
        String sql = "select pid from customer where phonenumber = ? and password = ?";
        return (Integer) queryForSingleValue(sql,phonenumber,password);

    }


    public int saveUser(customer customer) throws Exception {
        String sql = "INSERT INTO customer (name, phonenumber, password) VALUES(?,?,?)";
        return update(sql,customer.getName(),customer.getPhoneNumber(),customer.getPassword());
    }

    public int updateCustomer(customer customer) {
        String sql = "update customer set name = ?, password = ?,phonenumber =  ? where pid = ? ";
        try {
            return update(sql,customer.getName(),customer.getPassword(),customer.getPhoneNumber(),customer.getPid());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
